/*
 * @Author: luthor luthor-li@qq.com
 * @Date: 2024-03-05 20:51:44
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-03-14 15:14:17
 * @FilePath: \allfront\src\utils\fpstream\utils\ueMsg.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export const useUemsg = stream => {
  const ueCbMap = new Map();
  const ueListenMap = new Map();
  //   处理ue监听通知
  const ueNotify = Notify => {
    console.log('Notify', Notify);
    const JsonRes = JSON.parse(Notify);
    const { cmd, data } = JsonRes;
    if (cmd && ueCbMap.has(cmd)) {
      // 处理send2Ue回调函数
      ueCbMap.get(cmd)(data);
      ueCbMap.delete(cmd);
      return;
    } else if (cmd && ueListenMap.has(cmd)) {
      // 处理listen2Ue回调函数
      ueListenMap.get(cmd)(data);
      ueListenMap.delete(cmd);
      return;
    }
  };
  stream.addResponseEventListener('handle_responses', ueNotify);
  //   发送数据到ue
  const send2Ue = (cmd, data, cb) => {
    stream.emitUIInteraction({
      cmd: cmd,
      data: data,
    });
    if (cb) ueCbMap.set(cmd, cb);
  };
  //   设置监听ue信息回调函数
  const listen2Ue = (cmd, cb) => {
    if (cb) ueListenMap.set(cmd, cb);
  };
  return {
    send2Ue,
    listen2Ue,
  };
};
